# -*- coding;utf-8 -*-
"""
File name : SaveImage.py.PY
Program IDE : PyCharm
Create file time: 2024/3/2 10:51
File Create By Author : 朱清闯
"""
from utils.ReadDocx2QuestionListVocational import get_new_name
import os
from django.conf import settings


# 保存图片文件到服务器，并返回该图片的url,
def save_image(images):
    try:
        file_info, suffix = os.path.splitext(images.name)
        if suffix.upper() not in ['.JPG', '.JPEG', '.PNG']:
            return False, '', ''
        # 保存文件
        new_name = get_new_name() + suffix
        # where将要保存的地址和文件名称
        where = os.path.join(settings.BASE_DIR).replace('\\', '/') + settings.IMAGE_URL + new_name

        # 分块保存image
        content = images.chunks()
        with open(where, 'wb') as f:
            for i in content:
                f.write(i)
        images_url = settings.SERVER_IP + settings.IMAGE_URL + new_name
        images_path = where
        return images_url, images_path
    except Exception as e:
        print(e)


# 保存临时用到的图片，目录在images_temp文件夹里面
def save_image_temp(images):
    try:
        file_info, suffix = os.path.splitext(images.name)
        if suffix.upper() not in ['.JPG', '.JPEG', '.PNG']:
            return False, '', ''
        # 保存文件
        new_name = get_new_name() + suffix
        # where将要保存的地址和文件名称
        where = os.path.join(settings.BASE_DIR).replace('\\', '/') + settings.IMAGE_TEMP_URL + new_name

        # 分块保存image
        content = images.chunks()
        with open(where, 'wb') as f:
            for i in content:
                f.write(i)
        images_url = settings.SERVER_IP + settings.IMAGE_URL + new_name
        images_path = where
        return images_url, images_path
    except Exception as e:
        print(e)


# 保存文档文件到服务器，并返回该文档的url,
def save_zip_or_xlsx(file):
    try:
        result_flag = True
        file_info, suffix = os.path.splitext(file.name)
        if suffix.upper() not in ['.DOCX', '.ZIP']:
            return None, None, False
        # 保存文件
        new_name = get_new_name() + suffix
        # where将要保存的地址和文件名称
        where = os.path.join(settings.BASE_DIR).replace('\\', '/') + settings.IMAGE_TEMP_URL + new_name
        # 分块保存文档
        content = file.chunks()
        with open(where, 'wb') as f:
            for i in content:
                f.write(i)
        file_url = settings.SERVER_IP + settings.IMAGE_TEMP_URL + new_name
        file_path_no_https = settings.IMAGE_TEMP_URL + new_name
        return file_url, file_path_no_https, result_flag
    except Exception as e:
        print('Exception:', e)
        return None, None, False


# 保存文档文件到服务器，并返回该文档的url,
def save_csv(file):
    try:
        result_flag = True
        file_info, suffix = os.path.splitext(file.name)
        if suffix.upper() not in ['.CSV']:
            return None, None, False
        # 保存文件
        new_name = get_new_name() + suffix
        # where将要保存的地址和文件名称
        where = os.path.join(settings.BASE_DIR).replace('\\', '/') + settings.IMAGE_TEMP_URL + new_name
        # 分块保存文档
        content = file.chunks()
        with open(where, 'wb') as f:
            for i in content:
                f.write(i)
        file_url = settings.SERVER_IP + settings.IMAGE_TEMP_URL + new_name
        file_path_no_https = settings.IMAGE_TEMP_URL + new_name
        return file_url, where, result_flag
    except Exception as e:
        print('Exception:', e)
        return None, None, False
